import "package:mobx/mobx.dart";
import "package:hinge/common/models/perfume_model.dart";
import "package:hinge/common/services/perfume_service.dart";
import "package:hinge/common/services/user_service.dart";

part "note_store.g.dart";

class NoteStore = _NoteStore with _$NoteStore;

abstract class _NoteStore with Store {
  @observable
  bool isLoading = false;

  @observable
  ObservableList<PerfumeModel> perfumes = ObservableList<PerfumeModel>.of([]);

  // 当前用户的香水列表
  @observable
  ObservableList<PerfumeModel> currentUserPerfumes =
      ObservableList<PerfumeModel>.of([]);

  // 其他用户的香水列表
  @observable
  ObservableList<PerfumeModel> otherUsersPerfumes =
      ObservableList<PerfumeModel>.of([]);

  // 是否为游客模式
  @observable
  bool isGuestMode = false;

  @action
  Future<void> loadPerfumes() async {
    isLoading = true;

    // 检查当前是否为游客模式
    isGuestMode = UserService.isGuestUser();

    await Future.delayed(const Duration(milliseconds: 300));
    final allPerfumes = PerfumeService.getAllPerfumes();

    perfumes.clear();
    currentUserPerfumes.clear();
    otherUsersPerfumes.clear();

    // 分离当前用户和其他用户的香水
    final currentUser = UserService.getCurrentUser();
    if (currentUser != null && !isGuestMode) {
      // 当前用户的香水
      currentUserPerfumes.addAll(
        allPerfumes.where((perfume) => perfume.userId == currentUser.id),
      );

      // 其他用户的香水
      otherUsersPerfumes.addAll(
        allPerfumes.where((perfume) => perfume.userId != currentUser.id),
      );
    } else {
      // 游客模式或用户未登录，将所有香水添加到其他用户列表
      otherUsersPerfumes.addAll(allPerfumes);
    }

    // 保持原有的perfumes列表兼容性
    perfumes.addAll(allPerfumes);

    isLoading = false;
  }
}
